package com.gemantic.task.consumer;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service;

import com.gemantic.task.service.TaskService;

@ConditionalOnExpression("${task.p2.enable:false}")
@Service
@RocketMQMessageListener(topic = "${task.p2.topic}", consumerGroup = "${task.p2.consumergroup}", consumeThreadMax = 4)
public class TaskConsumer2 implements RocketMQListener<Long> {
    private final static String RUNNER_NAME_PREFIX = "mq_task";
    @Autowired
    private TaskService taskService;

    @Override
    public void onMessage(Long id) {
        String name = Thread.currentThread().getName();
        String runnerName = String.format("%s_%s", RUNNER_NAME_PREFIX, name);
        taskService.runTask(id, runnerName);
    }
}
